package com.jiahao.material.controllers;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiahao.material.entity.*;
import com.jiahao.material.services.IRoleService;
import com.jiahao.material.services.ISupplierService;
import com.jiahao.material.utils.ResultUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private ISupplierService supplierService;

    @RequiresPermissions("addSupplier")
    @PostMapping("/add")
    public Result add(@RequestBody Supplier supplier) throws Exception{
        supplier.setStatus("01");
        supplierService.save(supplier);
        return ResultUtil.success("添加供应商成功!");
    }

    @GetMapping("/byId/{id}")
    public Result getById(@PathVariable String id) throws Exception{
        Supplier byId = supplierService.getById(id);
        return ResultUtil.success("",byId);
    }

    /**
     * 分页查询
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/page")
    public Result getByPage(@RequestBody Map<String,Object> map) throws Exception{
        int pageSize = map.get("pageSize") == null ? 10 : Integer.parseInt(map.get("pageSize").toString());
        int currentPage = map.get("currentPage") == null ? 1 : Integer.parseInt(map.get("currentPage").toString());
        Page<Supplier> page = new Page<>(currentPage,pageSize);
        IPage<Supplier> page1 = supplierService.getByPage(page, map);
        return ResultUtil.success("",page1);
    }

    @GetMapping("/activeAll")
    public Result getActiveAll() throws Exception{
        QueryWrapper<Supplier> supplierQueryWrapper = new QueryWrapper<Supplier>();
        supplierQueryWrapper.eq("status","01");
        List<Supplier> list = supplierService.list(supplierQueryWrapper);
        return ResultUtil.success("",list);
    }

    @GetMapping("/all")
    public Result getAll() throws Exception{
        List<Supplier> list = supplierService.list();
        return ResultUtil.success("",list);
    }

    /**
     * 校验消费者的名称
     * @param map
     * @return
     * @throws Exception
     */
    @PostMapping("/checkName")
    public Result checkName(@RequestBody Map<String,Object> map) throws Exception{
        QueryWrapper<Supplier> supplierQueryWrapper = new QueryWrapper<>();
        supplierQueryWrapper.eq("name",map.get("name").toString());
        Supplier one = supplierService.getOne(supplierQueryWrapper);
        String e="0";
        if (one!=null)
            e = one.getId().equals(map.get("id").toString())?"0":"1";
        return ResultUtil.success(e);
    }

    /**
     * * 更新状态
     * @param map
     * @return
     * @throws Exception
     */
    @RequiresPermissions("updateSupplier")
    @PutMapping("/status")
    @Transactional
    public Result updateStatus(@RequestBody Map<String,Object> map) throws Exception{
        UpdateWrapper<Supplier> supplierUpdateWrapper = new UpdateWrapper<>();
        supplierUpdateWrapper.eq("id",map.get("id").toString()).set("status",map.get("status").toString());
        supplierService.update(supplierUpdateWrapper);
        return ResultUtil.success("更新状态成功!");
    }

    @RequiresPermissions("updateSupplier")
    @PutMapping("/update")
    public Result update(@RequestBody Supplier supplier) throws Exception{
        supplierService.updateById(supplier);
        return ResultUtil.success("更新供应商成功!");
    }

    @RequiresPermissions("delSupplier")
    @DeleteMapping("/del/{id}")
    public Result delById(@PathVariable String id) throws Exception{
        supplierService.removeById(id);
        return ResultUtil.success("删除供应商成功!");
    }

    @RequiresPermissions("delSupplier")
    @PostMapping("/dels")
    public Result delByIds(@RequestBody List<String> ids) throws Exception{
        supplierService.removeByIds(ids);
        return ResultUtil.success("删除仓库人员成功!");
    }

}
